{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test add_annotations_to_docx\n",
    "\n",
    "This notebook tests the `add_annotations_to_docx` function from `src/williamtoolbox/annotation.py`.\n",
    "\n",
    "## Function Overview\n",
    "\n",
    "The `add_annotations_to_docx` function adds annotations to a Word document. It takes two parameters:\n",
    "- file_path: Path to the Word document\n",
    "- annotations: List of Annotation objects containing text and comment pairs\n",
    "\n",
    "## Setup\n",
    "\n",
    "First, let's import necessary modules and create a test document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test document created at: /Users/allwefantasy/Downloads/测试.docx\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "from docx import Document\n",
    "from williamtoolbox.annotation import add_annotations_to_docx, Annotation\n",
    "\n",
    "# Create a test directory\n",
    "test_dir = Path(\"/Users/allwefantasy/Downloads\")\n",
    "test_dir.mkdir(exist_ok=True)\n",
    "\n",
    "# Create a simple test document\n",
    "test_doc_path = test_dir / \"测试.docx\"\n",
    "doc = Document()\n",
    "doc.add_paragraph(\"This is a test paragraph.\")\n",
    "doc.add_paragraph(\"Another paragraph for testing.\")\n",
    "doc.save(test_doc_path)\n",
    "\n",
    "print(f\"Test document created at: {test_doc_path}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Case 1: Basic Annotation\n",
    "\n",
    "Let's test adding a single annotation to the document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_dir' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 16\u001b[0m\n\u001b[1;32m     13\u001b[0m add_annotations_to_docx(\u001b[38;5;28mstr\u001b[39m(test_doc_path), annotations)\n\u001b[1;32m     15\u001b[0m \u001b[38;5;66;03m# Verify the output\u001b[39;00m\n\u001b[0;32m---> 16\u001b[0m output_path \u001b[38;5;241m=\u001b[39m \u001b[43mtest_dir\u001b[49m \u001b[38;5;241m/\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtest_document_annotated.docx\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m     17\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAnnotated document saved to: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00moutput_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'test_dir' is not defined"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "from docx import Document\n",
    "from williamtoolbox.annotation import add_annotations_to_docx, Annotation\n",
    "\n",
    "# Create test annotations\n",
    "test_doc_path = Path(\"/Users/allwefantasy/Downloads/测试.docx\")\n",
    "annotations = [\n",
    "    Annotation(text=\"This is a test paragraph.\", comment=\"This is a test comment\")\n",
    "]\n",
    "\n",
    "# Add annotations to document\n",
    "add_annotations_to_docx(str(test_doc_path), annotations)\n",
    "\n",
    "# Verify the output\n",
    "print(f\"Annotated document saved to: {test_doc_path}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Annotated document saved to: /Users/allwefantasy/Downloads/测试.docx\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "from docx import Document\n",
    "from williamtoolbox.annotation import add_annotations_to_docx_v2, Annotation\n",
    "\n",
    "# Create test annotations\n",
    "test_doc_path = Path(\"/Users/allwefantasy/Downloads/测试.docx\")\n",
    "annotations = [\n",
    "    Annotation(text=\"This is a test paragraph.\", comment=\"This is a test comment\")\n",
    "]\n",
    "\n",
    "# Add annotations to document\n",
    "add_annotations_to_docx_v2(str(test_doc_path), annotations)\n",
    "\n",
    "# Verify the output\n",
    "print(f\"Annotated document saved to: {test_doc_path}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Case 2: Multiple Annotations\n",
    "\n",
    "Test adding multiple annotations to different paragraphs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create multiple annotations\n",
    "annotations = [\n",
    "    Annotation(text=\"This is a test paragraph\", comment=\"First comment\"),\n",
    "    Annotation(text=\"Another paragraph for testing\", comment=\"Second comment\")\n",
    "]\n",
    "\n",
    "# Add annotations\n",
    "add_annotations_to_docx(str(test_doc_path), annotations)\n",
    "\n",
    "# Verify the output\n",
    "output_path = test_dir / \"test_document_multiple_annotated.docx\"\n",
    "print(f\"Annotated document saved to: {output_path}\")\n",
    "\n",
    "# Open and check the document\n",
    "doc = Document(output_path)\n",
    "for para in doc.paragraphs:\n",
    "    print(para.text)\n",
    "    if para._element.xpath('.//w:comment'):\n",
    "        print(\"Found comment in paragraph:\", para.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Case 3: Error Handling\n",
    "\n",
    "Test error cases such as non-existent file and empty annotations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytest\n",
    "\n",
    "# Test non-existent file\n",
    "try:\n",
    "    add_annotations_to_docx(\"non_existent.docx\", annotations)\n",
    "except Exception as e:\n",
    "    print(f\"Expected error for non-existent file: {str(e)}\")\n",
    "\n",
    "# Test empty annotations\n",
    "try:\n",
    "    add_annotations_to_docx(str(test_doc_path), [])\n",
    "    print(\"No error for empty annotations\")\n",
    "except Exception as e:\n",
    "    print(f\"Error for empty annotations: {str(e)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cleanup\n",
    "\n",
    "Remove test files after testing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove test files\n",
    "for file in test_dir.glob(\"*\"):\n",
    "    file.unlink()\n",
    "test_dir.rmdir()\n",
    "print(\"Test files cleaned up\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
